<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="财务年度">
          <a-date-picker placeholder="请输入财务年度" 
      mode="year"
      format="YYYY"
      :open="open"
      @openChange="openChange"
      @panelChange="panelChange" v-decorator="['FiscalYear', validatorRules.fiscalyear]" />
            
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="营业收入">
          <a-input-number style="width:200px;" placeholder="请输入营业收入" v-decorator="['IncomeAmt', {}]" />
          <span style="margin-left: 5px;">万元</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="营业利润">
          <a-input-number style="width:200px;" placeholder="请输入营业利润" v-decorator="['ProfitAmt', {}]" />
          <span style="margin-left: 5px;">万元</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总资产">
          <a-input-number style="width:200px;" placeholder="请输入总资产" v-decorator="['AssetsAmt', {}]" />
          <span style="margin-left: 5px;">万元</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总负债">
          <a-input-number style="width:200px;" placeholder="请输入总负债" v-decorator="['LiabilitiesAmt', {}]" />
          <span style="margin-left: 5px;">万元</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="实缴税收">
          <a-input-number style="width:200px;" placeholder="请输入实缴税收" v-decorator="['TaxedAmt', {}]" />
          <span style="margin-left: 5px;">万元</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="人员规模">
          <a-select placeholder="请输入人员规模" v-decorator="['StaffSizeId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in staffData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="数据来源">
          <a-select placeholder="请输入数据来源" v-decorator="['DataSourceId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in dataSourceData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyOperationModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        open: false,
        year: moment(),
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        companyData:[],
        staffData:[{
          id:'14',
          name:'0-50人'
        },{
          id:'15',
          name:'50-150人'
        },{
          id:'16',
          name:'150-500人'
        },{
          id:'17',
          name:'500-1000人'
        },{
          id:'18',
          name:'1000-5000人'
        },{
          id:'19',
          name:'5000-10000人'
        },{
          id:'20',
          name:'10000人以上'
        }],
        dataSourceData:[{
          id:'158',
          name:'政府授权'
        },{
          id:'159',
          name:'企业授权'
        },{
          id:'160',
          name:'第三方机构授权'
        }],
        fiscalData:[],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companyoperationid:{rules: [{ required: true, message: '请输入经营状况ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        fiscalyear:{rules: [{ required: true, message: '请输入财务年度!' }]},
        incomeamt:{rules: [{ required: true, message: '请输入营业收入!' }]},
        profitamt:{rules: [{ required: true, message: '请输入营业利润!' }]},
        assetsamt:{rules: [{ required: true, message: '请输入总资产!' }]},
        liabilitiesamt:{rules: [{ required: true, message: '请输入总负债!' }]},
        taxedamt:{rules: [{ required: true, message: '请输入实缴税收!' }]},
        staffsizeid:{rules: [{ required: true, message: '请输入人员规模!' }]},
        datasourceid:{rules: [{ required: true, message: '请输入数据来源!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        },
        url: {
          add: "/companyOperation/add",
          edit: "/companyOperation/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      // this.getCompany();
      this.getBaseData();
    },
    methods: {
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'DataSources'
            }]}).then((res)=>{
          if(res.status == 1){
            this.dataSourceData = res.data.rows;
          }
        })
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'StaffSize'
            }]}).then((res)=>{
          if(res.status == 1){
            this.staffData = res.data.rows;
          }
        })
      },
      openChange(status) {
        if (status) {
          this.open = true;
        } else {
          this.open = false;
        }
        this.$forceUpdate();
      },
      panelChange(value){
        this.form.setFieldsValue({
          FiscalYear:value
        })
        // this.year = value;
        this.open = false;
        this.$forceUpdate();
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.CompanyId = this.model.Company;
        this.model.StaffSizeId = this.model.StaffSize;
        this.model.DataSourceId = this.model.DataSource;
        if(!this.model.FiscalYear){
          this.model.FiscalYear = (new Date()).getFullYear();
        }
        this.model.FiscalYear = this.model.FiscalYear+"-01-01 00:00:00";
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyId','IncomeAmt','ProfitAmt','AssetsAmt','LiabilitiesAmt','TaxedAmt','StaffSizeId','DataSourceId','Company_CompanyName'))
		  //时间格式化
      this.form.setFieldsValue({FiscalYear:this.model.FiscalYear?moment(this.model.FiscalYear,'YYYY'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyOperationId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.FiscalYear = formData.FiscalYear?formData.FiscalYear.format('YYYY'):null;
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>